package com.ang.jdk8.stream;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ssang
 * @date 2022-03-02 13:59
 */
public class PeekTest {

    public static void main(String[] args) {
        PeekTest t = new PeekTest();
        t.test2();
    }

    /**
     * 感觉和map差不多
     * map里面是return，peek不需要return
     *
     * map：用于对流中的每个元素进行映射处理，然后再形成新的流；
     * peek：用于 debug 调试流中间结果，不能形成新的流，但能修改引用类型字段的值；
     */
    public void test1(){
        User u1 = new User(1,0,"a1");
        User u2 = new User(2,0,"a2");

        List<User> list = new ArrayList<>(Arrays.asList(u1,u2));

        list.stream().peek(u->{
            u.setName(String.format("name:%s",u.name));
        }).collect(Collectors.toList());

        list.stream().forEach(user -> {
            System.out.println(user.name);
        });
    }

    /**
     * 来做一个递归
     */
    public void test2(){
        User u1 = new User(1,0,"a1");
        User u2 = new User(2,1,"a1-2");
        User u3 = new User(3,1,"a1-3");
        User u4 = new User(4,2,"a2-4");
        User u5 = new User(5,2,"a2-5");
        User u6 = new User(6,1,"a1-6");
        User u7 = new User(7,3,"a3-7");
        User u8 = new User(8,3,"a3-8");
        User u9 = new User(9,4,"a4-9");

        List<User> list = new ArrayList<>(Arrays.asList(u1,u2,u3,u4,u5,u6,u7,u8,u9));

        List<User> resultList = list.stream().filter(user ->
                user.getpId() == 0
            )
            .peek(user -> {
                System.out.println(user.toString());
                user.setChildrenList(this.loopChild(user,list));
            }).collect(Collectors.toList());

        System.out.println(resultList.size());
    }

    /**
     * 递归方法
     */
    public List<User> loopChild(User parent, List<User> list){
        return list.stream().filter(user ->
            user.getpId() == parent.getId()
        )
        .peek(user -> {
            user.setChildrenList(loopChild(user, list));
        }).collect(Collectors.toList());
    }

    /**
     * 内部类对象
     */
    class User{
        private int id;
        private int pId;
        private String name;
        private List<User> childrenList;

        public User(int id, int pId, String name){
            this.id = id;
            this.pId = pId;
            this.name = name;
        }

        public String toString(){
            return String.format("id:%s,pId:%s,name:%s",this.id,this.pId,this.name);
        }

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public int getpId() {
            return pId;
        }

        public void setpId(int pId) {
            this.pId = pId;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public List<User> getChildrenList() {
            return childrenList;
        }

        public void setChildrenList(List<User> childrenList) {
            this.childrenList = childrenList;
        }
    }
}
